quietly{
version 16

*For laptop:
use "C:\Users\achen.POL-PF10YAEL\Documents\Word\citizen assemblies\Krashinsky project\Ontario data\ontario 2007.dta"

* USE THIS ONE FOR LAPTOP
cd "C:\Users\achen.POL-PF10YAEL\Documents\Word\citizen assemblies\Krashinsky project\Ontario data"

*THIS ONE IS FOR WORK DESKTOP
*cd "C:\Users\achen\Documents\Word\citizen assemblies\Krashinsky project\Ontario data"

*IS THIS THE LOG FILE YOU WANT?
log using ontario2007finalrun.txt, replace text

* incumbent Libs had won 46 to NDP's 33 in 2005. This survey is pre-election 2009.  
* Eventual 2009 votes were 45.8 Lib, 42.2 NDP, 8.2 Green, Tories 1.5, Indep 1.05, 
* rest each < .5

gen weight = HHWGT
* Code intended 2007 referendum vote yes/no

clonevar choicereforig = REFV3
gen choiceref = choicereforig
replace choiceref = 1 if choicereforig == 5
replace choiceref = 0 if choicereforig == 1
replace choiceref = . if choicereforig>5
label define choiceref 0 "no" 1 "yes"
label values choiceref choiceref
*Likelihood to vote:
clonevar votelikely = REFV1
replace votelikely = . if REFV1==8
replace votelikely = (5 - votelikely)/4
*Next variable is intended provincial vote in 2009.
* There is a follow-up about "lean toward," but not included here.
clonevar choiceprovorig = REFV2
gen choiceprov = choiceprovorig
replace choiceprov = . if choiceprovorig>4 |choiceprovorig==0
replace choiceprov = 5 if REFV2==8
label values choiceprov REFV2
gen libchoice = 0
replace libchoice = 1 if choiceprov==1
gen pcchoice = 0
replace pcchoice = 1 if choiceprov==2
gen ndpchoice = 0
replace ndpchoice = 1 if choiceprov==3
gen greenchoice = 0
replace greenchoice = 1 if choiceprov==4
label define choiceprov 1 "Liberals" 2 "PCs" 3 "NDP" 4 "Greens" 5 "DK"
label values choiceprov choiceprov

clonevar femaleorig = RGENDER
gen female = 0
replace female = 1 if femaleorig==5
replace female = . if femaleorig==.

*Correct answer on next one is yes=1; DK and refused coded as wrong answer throughout.
*Also respondents were not asked if they said they knew "nothing"--also coded as
*"no info" on each of the following:
gen refinfo1 = 0
replace refinfo1 = 1 if REFA3==1
gen refinfo2 = 0
replace refinfo2 = 1 if REFA4A == 2 | REFA4B==1
gen refinfo3 = 1
replace refinfo3 = 0 if REFB2!=1
*Correct answer is no on next one:
gen refinfo4 = 1
replace refinfo4 = 0 if REFB3!=5
*Again, yes:
gen refinfo5 = 1
replace refinfo5 = 0 if REFB4!=1
gen refinfo6 =1
replace refinfo6 = 0 if REFD10 != 1

gen refinfo = (refinfo1 + refinfo2 + refinfo3 + refinfo4 + refinfo5 + refinfo6)/6
gen refinfoshort = (refinfo1 + refinfo2 + refinfo3 )/3
clonevar votereflikelyorig = REFV3
gen votereflikely = votereflikelyorig
*code DK, refused as very unlikely; need to reverse scale, too:
replace votereflikely = 4 if votereflikelyorig>4
replace votereflikely = 1 - (votereflikely - 1)/3
clonevar refinterestorig = REFD2
gen refinterest = refinterestorig
*code DK, refused as "no interest" in referendum:
replace refinterest = 0 if refinterestorig>10
replace refinterest = refinterest/10

*Party knowledge questions.  Libs & Tories coded anti; NDP pro.
*No Greens question in data. These are asked about the leaders.

gen libknow = 1
replace libknow = 0 if REFE1 != 5
gen pcknow = 1
replace pcknow = 0 if REFE2 != 5
gen ndpknow = 1
replace ndpknow = 0 if REFE3 != 1

gen partyknow = (libknow+pcknow+ndpknow)/3

gen polinfo1 = 1
replace polinfo1 = 0 if REFF1 != 1
gen polinfo2 = 1
replace polinfo2 = 0 if REFF2 != 1
gen polinfo3 = 1
replace polinfo3 = 0 if REFF3 != 5
gen polinfo = (polinfo1 + polinfo2 + polinfo3 )/3

*Code trust as zero if DK or refused:
gen trustcitassmb = 1
replace trustcitassmb = 0 if REFG2!=1
*Also try with DK and refused = missing:
gen trustcitassmbnew = trustcitassmb
replace trustcitassmbnew = . if REFG2>5

/*
*Now some items about values, with DK, refused = no:
* (not yet coded--coding below is not correct)
gen promultiparty = 1
replace promultiparty = 0 if C1a!= 1 & C1b!=2
gen proproportionality = 1
replace proproportionality = 0 if C2!=5
gen prochoices = 1
replace prochoices = 0 if C5a !=2 & C5b!=1
gen proprefs = (promultiparty + proproportionality + prochoices)/3

*Now therms.
*DK, refused coded as 50 (midpoint):
* (not yet coded--codes below incorrect)  Have PID in this survey instead.
gen libthermorig = H1
gen libtherm = libthermorig
replace libtherm = 50 if libthermorig > 100
replace libtherm = libtherm/100
gen ndpthermorig = H2
gen ndptherm = ndpthermorig
replace ndptherm = 50 if ndpthermorig > 100
replace ndptherm = ndptherm/100
gen greenthermorig = H3
gen greentherm = greenthermorig
replace greentherm = 50 if greenthermorig > 100
replace greentherm = greentherm/100
*/

*For PID, code Green (only 4 Green identifiers) and refused as missing.
*Code other and DK as none.
* Use provincial PID, not federal:
clonevar pidorig = K1
gen pid = pidorig
replace pid = 0 if pidorig>3
replace pid = . if K1==4 | K1==9
label values pid K1
gen pidlib = 0 if pid!=.
replace pidlib = 1 if pid==1
gen pidpc = 0 if pid!=.
replace pidpc = 1 if pid==2
gen pidndp = 0 if pid!=.
replace pidndp = 1 if pid==3

*No Green knowledge question asked, hence have only three of these:
gen pidlibknow = 0
replace pidlibknow = 1 if pidlib==1 & libknow==1
gen pidpcknow = 0
replace pidpcknow = 1 if pidpc==1 & pcknow==1
gen pidndpknow = 0
replace pidndpknow = 1 if pidndp==1 & ndpknow==1
gen ownpartyknow = pidlibknow + pidpcknow + pidndpknow

gen libpolinfo = pidlib*polinfo
gen pcpolinfo = pidpc*polinfo
gen majorpolinfo = (pidlib + pidpc)*polinfo
gen ndppolinfo = pidndp*polinfo
gen indeppolinfo = 0
replace indeppolinfo = polinfo if pidpc+pidlib+pidndp==0

gen ndpchoiceinfo = ndpchoice*polinfo

gen libchoiceknow = 0
replace libchoiceknow = 1 if libchoice==1 & libknow==1
gen pcchoiceknow = 0
replace pcchoiceknow = 1 if pcchoice==1 & pcknow==1
gen ndpchoiceknow = 0
replace ndpchoiceknow = 1 if ndpchoice==1 & ndpknow==1

gen dob = DM1
gen age = 2007 - dob
replace age = . if dob>9990
*make age 0-1, corresponding to 18-100
replace age = (age-18)/82
gen educorig = DM2
gen educ = educorig
replace educ = . if educorig>10
* fix the apparent coding error for the one person with no education
* to match codebook:
replace educ = 1 if educorig==0
*scale to 0-1:
replace educ = (educ - 1)/8

gen incomeorig = DM11
gen income = incomeorig
replace income = 200 if income>200
replace income = . if incomeorig>997
gen incmissing = 0
replace incmissing = 1 if income==.
replace income = 0 if income==.
replace income = income/200

*copy in from supplemental income question
gen incomesuppl = DM11E
*Skip next section due to too much missing on DM11E
/*
replace income = 10 if incomesuppl ==1
replace income = 25 if incomesuppl ==2
replace income = 35 if incomesuppl ==3
replace income = 45 if incomesuppl ==4
replace income = 55 if incomesuppl ==5
replace income = 65 if incomesuppl ==6
replace income = 75 if incomesuppl ==7
replace income = 85 if incomesuppl ==8
replace income = 95 if incomesuppl ==9
replace income = 110 if incomesuppl ==10
replace income = 135 if incomesuppl ==11
* ">150K" coded as 200K, since that is approx. mean for those who answered
* DM11 & were >150
*/

}
*What did they know about party positions?
tab REFE1 [aweight= weight]
tab REFE2 [aweight= weight]
tab REFE3 [aweight= weight]
*Now for raw N's:
tab REFE1
tab REFE2
tab REFE3

*Compare with fig. 9 from Stephenson data on vote by whether knew party position:
*Liberals look odd (did not take position).
bysort pid: tab choiceref ownpartyknow [aweight = weight], col

tab choiceref pid [aweight=weight], col
tab choiceref choiceprov [aweight=weight], col
tab choiceref refinfo [aweight=weight], col
tab choiceref refinfo [aweight=weight] if pid==1 | pid==2, col
tab choiceref polinfo [aweight=weight], col
tab choiceref polinfo [aweight=weight] if pid==1 | pid==2, col
tab choiceref polinfo [aweight=weight] if pid==3, col
tab choiceref polinfo [aweight=weight] if pid==0, col

*Trust cit assemb vs sophistication
tab trustcitassmb polinfo [aweight=weight] if pid==3, col
bysort pid: tab trustcitassmb polinfo [aweight=weight] if pid!=3 , col
tab trustcitassmb polinfo [aweight=weight] if pid!=3 , col
*Now get N's:
tab trustcitassmb polinfo  if pid==3
tab trustcitassmb polinfo  if pid!=3
gen nonndp = 1
replace nonndp = 0 if pid==3
gen nonndppolinfo = nonndp*polinfo1
reg  trustcitassmb polinfo nonndp nonndppolinfo[pweight=weight] 
test nonndp nonndppolinfo
reg  trustcitassmb polinfo  nonndppolinfo[pweight=weight]
probit  trustcitassmb polinfo nonndp nonndppolinfo[pweight=weight] 
test nonndp nonndppolinfo
probit  trustcitassmb polinfo  nonndppolinfo[pweight=weight] 

*Now redo with trustcitassmbnew:
tab trustcitassmbnew polinfo [aweight=weight] if pid==3, col
bysort pid: tab trustcitassmbnew polinfo [aweight=weight] if pid!=3 , col
tab trustcitassmbnew polinfo [aweight=weight] if pid!=3 , col
*Now get N's:
tab trustcitassmbnew polinfo  if pid==3
tab trustcitassmbnew polinfo  if pid!=3
*Following four commands dropped because NDP plot not very linear (unlike previous
*coding of trustcitassmb):
*reg  trustcitassmbnew polinfo nonndp nonndppolinfo[pweight=weight] 
*test nonndp nonndppolinfo
*reg  trustcitassmbnew polinfo  nonndppolinfo[pweight=weight]
*probit  trustcitassmbnew polinfo nonndp nonndppolinfo[pweight=weight] 
*test nonndp nonndppolinfo
*probit  trustcitassmbnew polinfo  nonndppolinfo[pweight=weight] 
*Test diff of means and slope for non-NDP:
replace nonndp = 1
replace nonndp = 0 if pid==3
svyset [pweight  = weight]
svy: tab trustcitassmbnew nonndp, col pearson
svy, subpop(nonndp): reg trustcitassmbnew polinfo
svy, subpop(nonndp): probit trustcitassmbnew polinfo

*Info levels by pid
*Note that these four are unweighted:
tabulate pid, summarize(refinfo)
tabulate pid, summarize(polinfo)
tabulate choiceref, summarize(refinfo)
tabulate choiceref, summarize(polinfo)

* Lib leader (which is what question asks about) took no position;
* other two leaders did, and Greens not asked.  Hence: 
probit choiceref educ age libchoice pcchoice ndpchoice greenchoice  [pweight=weight]
fitstat
probit choiceref educ age pidlib pidpc pidndp polinfo [pweight=weight]
fitstat

probit choiceref educ age pidlib pidpc pidndp pidpcknow pidndpknow polinfo [pweight=weight]
fitstat

*This is the first ones to look at, but use party id ones below:
probit choiceref educ age i.libchoice i.pcchoice i.ndpchoice i.greenchoice polinfo ndpchoiceinfo [pweight=weight]
fitstat
eststo dydx_choiceref1: margins, dydx(*) post
*Check on income and gender:
probit choiceref educ age i.libchoice i.pcchoice i.ndpchoice i.greenchoice polinfo ndpchoiceinfo income i.incmissing i.female [pweight=weight]
fitstat
eststo dydx_choiceref1: margins, dydx(*) post
*Following not used due to endogeneity of know variables:
probit choiceref educ age libchoice pcchoice ndpchoice greenchoice libchoiceknow pcchoiceknow ndpchoiceknow  polinfo [pweight=weight]
fitstat
probit choiceref educ age libchoice pcchoice ndpchoice greenchoice pcchoiceknow ndpchoiceknow  polinfo [pweight=weight]
fitstat
probit choiceref educ age pidlib pidpc pidndp pidpcknow pidndpknow majorpolinfo ndppolinfo indeppolinfo [pweight=weight]
fitstat

/*
*No longer used due to endogeneity of knowing party position:
probit choiceref age i.pidlib i.pidpc i.pidndp i.i.pidpcknow i.pidndpknow polinfo [pweight=weight]
fitstat
eststo dydx_choiceref: margins, dydx(*) post
coefplot, drop(_cons) xline(0) rename(1.pidlib = "Liberal PID" 1.pidpc = "PC PID" 1.pidndp = "NDP PID" 1.pidpcknow = "PC know party view"  1.pidndpknow = "NDP know party view" polinfo = "political information") title(Marginal Effects on Favoring Electoral Change) subti(Ontario 2007)
graph export fig4.pdf,  logo(off) replace
*/

*This is the set to use:
probit choiceref educ age  i.pidlib i.pidpc i.pidndp polinfo ndppolinfo [pweight=weight]
fitstat
eststo dydx_choiceref2: margins, dydx(*) post
*Check on income and gender:
probit choiceref educ age  i.pidlib i.pidpc i.pidndp polinfo ndppolinfo income i.incmissing i.female [pweight=weight]
fitstat
eststo dydx_choiceref2: margins, dydx(*) post

probit choiceref educ age  i.pidlib i.pidpc i.pidndp polinfo majorpolinfo [pweight=weight]
fitstat
eststo dydx_choiceref2: margins, dydx(*) post
*Check on income and gender:
probit choiceref educ age  i.pidlib i.pidpc i.pidndp polinfo majorpolinfo income i.incmissing i.female [pweight=weight]
fitstat
eststo dydx_choiceref2: margins, dydx(*) post

*coefplot, drop(_cons) xline(0) rename(1.pidlib = "Liberal PID" 1.pidpc = "PC PID" 1.pidndp = "NDP PID" ndppolinfo = "NDP political information" polinfo = "political information") title(Marginal Effects on Favoring Electoral Change) subti(Ontario 2007)
*graph export fig5.pdf,  logo(off) replace

* GRAPHS
graph bar (mean) choiceref [aweight = weight], over(choiceprov) ytitle(fraction yes) ti(Ontario 2007) subti(Fournier et al. survey) ylabel(0(.2).8)
graph export fig3.pdf, logo(off) replace
graph save fig3, replace

*Validation for remark that Ontario looks like BC 2009 in fig. 2:
tab choiceref refinfo [aweight=weight], col

log close

